#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from typing import Union

from sqlalchemy import String, ForeignKey
from sqlalchemy.orm import Mapped, mapped_column

from backend.app.core.models.base import Base, id_key


class DbmsParamTemplate(Base):
    """系统api"""
    __tablename__ = 'dbms_param_template'

    id: Mapped[id_key] = mapped_column(init=False)
    instance_id: Mapped[int | None] = mapped_column(
        ForeignKey('dbms_param_template.id', ondelete='SET NULL'), default=None, comment='标签关联ID'
    )
    db_type: Mapped[int] = mapped_column(default=3306, comment='角色状态（3306 mysql 1 6379 redis 9200 es 1433 sql）')
    variable_name: Mapped[str] = mapped_column(String(50), unique=True, comment='tag代码')
    default_value: Mapped[str] = mapped_column(String(50), unique=True, comment='tag代码')
    editable: Mapped[int] = mapped_column(default=1, comment='角色状态（0停用 1正常）')
    is_restart: Mapped[int] = mapped_column(default=1, comment='角色状态（0停用 1正常）')
    valid_values: Mapped[str] = mapped_column(String(50), unique=True, comment='tag代码')
    status: Mapped[int] = mapped_column(default=1, comment='角色状态（0停用 1正常）')
    remark: Mapped[str | None] = mapped_column(String(255), comment='备注')



class DbmsParam(Base):
    __tablename__ = 'dbms_instance_param'
    """
    可在线修改的动态参数配置
    """
    instance_id = Column(Integer,  comment='实例ID')
    variable_name = CharField('参数名', max_length=64)
    old_var = CharField('修改前参数值', max_length=1024)
    new_var = CharField('修改后参数值', max_length=1024)
    set_sql = CharField('在线变更配置执行的SQL语句', max_length=1024)
    create_by = Column(String(64), default='', comment='创建者')
    create_time = Column(DateTime, comment='创建时间', default=datetime.now())
    update_by = Column(String(64), default='', comment='更新者')
    update_time = Column(DateTime, comment='更新时间', default=datetime.now())
    remark = Column(String(255), comment='备注')